package com.dong.HashMap.arithemtic;

import java.util.HashSet;

/**
 * @author dcz
 * @classname Demo3_method2
 * @description 快乐数
 * @date 2022/4/20 9:43
 */
public class Demo3_method2 {
    public boolean isHappy(int n){
        HashSet<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)){
            record.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }


    //拆解数，求平方和
    private int getNextNumber(int n) {
        int sum = 0;
        while (n>0){
            int temp = n % 10;
            sum = temp*temp;
            n = temp/10;
        }
        return sum;
    }
}
